package RPPM.servlet;

import RPPM.dao.impl.ExecutorDaoImpl;
import RPPM.domain.ResultInfo;
import RPPM.domain.Teacher;
import RPPM.service.ExeService;
import RPPM.service.Impl.ExeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@WebServlet("/exeservlet/*")
public class ExeServlet extends BaseServlet{
    public void exeCheckCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("exeCheckCourse");

        int exe_id = (int) request.getSession().getAttribute("exeid");
        System.out.println(exe_id);

        ExeService exeService = new ExeServiceImpl();
        List<Map<String, Object>> coursesPlan = exeService.findCoursesByExeid();

        ResultInfo info = new ResultInfo();
        info.setData(coursesPlan);
        writeJsonValue(response, info);
    }
    public void exesearchCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("exesearchCourse");

        String searchtext = request.getParameter("searchText");
        System.out.println(searchtext);

        ExeService exeService = new ExeServiceImpl();
        List<Map<String, Object>> coursesPlan = exeService.findCoursesByCouname(searchtext);


        ResultInfo info = new ResultInfo();
        info.setData(coursesPlan);
        writeJsonValue(response, info);
    }
    public void getNoPassPlan(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("getNoPassPlan");


        ExeService exeService = new ExeServiceImpl();
        List<Map<String, Object>> coursesPlan = exeService.getNoPassPlan();


        ResultInfo info = new ResultInfo();
        info.setData(coursesPlan);
        writeJsonValue(response, info);
    }
    public void passPlan(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("passPlan");

        int count = Integer.parseInt(request.getParameter("count"));
        System.out.println(count);

        ExeService exeService = new ExeServiceImpl();
        Map<String, Object> map = exeService.getNoPassPlan().get(count-1);
        System.out.println(map);
        boolean flag = exeService.passPlan(map);


        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        writeJsonValue(response, info);
    }
    public void searchPlan(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("searchPlan");

        String searchtext = request.getParameter("searchText");
        System.out.println(searchtext);

        ExeService exeService = new ExeServiceImpl();
        List<Map<String, Object>> coursesPlan = exeService.findCoursesNotPassedByCouname(searchtext);


        ResultInfo info = new ResultInfo();
        info.setData(coursesPlan);
        writeJsonValue(response, info);
    }
}
